/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECovoiturage. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.core.service.impl;

import static com.google.gwt.thirdparty.guava.common.base.Preconditions.checkArgument;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.smouquet.ecovoiturage.core.exception.MailException;
import com.smouquet.ecovoiturage.core.service.ConfigService;
import com.smouquet.ecovoiturage.core.service.MailService;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

/**
 * Implémentation de {@link MailService}.
 * 
 * @author sebastien.mouquet
 */
public class MailServiceImpl implements MailService
{
    @Inject
    @Named(ConfigService.PROPERTY_MAIL_SMTP_HOST)
    private String smtp;
    

    @Override
    public void sendMail(List<String> recipients, String object, String message) throws MailException
    {
        checkArgument(recipients != null, "to is required");
        checkArgument(!recipients.isEmpty(), "to must have at least one element");

        
        try
        {
            Properties props = new Properties();
            props.put(ConfigService.PROPERTY_MAIL_SMTP_HOST, smtp);

            Session session = Session.getInstance(props);

            InternetAddress[] destinataires = new InternetAddress[recipients.size()];
            int i = -1;
            for(String adr : recipients) {
                destinataires[++i] = new InternetAddress(adr);
            }

            Message mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(FROM_ADDRESS));
            mimeMessage.setRecipients(Message.RecipientType.TO, destinataires);
            mimeMessage.setSubject(MimeUtility.encodeText(object, "UTF-8", "Q"));
            mimeMessage.addHeader("X-Mailer", "ECovoiturage");
            mimeMessage.setContent(message, "text/html; charset=\"utf-8\"");

            Transport.send(mimeMessage);
            
        } catch(Exception ex) {
            throw new MailException(ex);
        }
    }

    @Override
    public void sendSeparateMail(List<String> recipients, String object, String message) throws MailException
    {
        checkArgument(recipients != null, "to is required");
        checkArgument(!recipients.isEmpty(), "to must have at least one element");
        
        
        Properties props = new Properties();
        props.put(ConfigService.PROPERTY_MAIL_SMTP_HOST, smtp);
        
        Session session;
        Message mimeMessage;
        int prob = 0;
        
        for(String adr : recipients)
        {
            try
            {
                session = Session.getInstance(props);

                mimeMessage = new MimeMessage(session);
                mimeMessage.setFrom(new InternetAddress(FROM_ADDRESS));
                mimeMessage.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(adr)});
                mimeMessage.setSubject(MimeUtility.encodeText(object, "UTF-8", "Q"));
                mimeMessage.addHeader("X-Mailer", "ECovoiturage");
                mimeMessage.setContent(message, "text/html; charset=\"utf-8\"");

                Transport.send(mimeMessage);

            } catch(Exception ex) {
                prob++;
            }
        }
        
        if(prob > 0) {
            throw new MailException("Mail doesn't send for "+prob+" recipients");
        }
    }
    
    
    
}
